class Member:
    """会员类，包含等级和折扣率"""

    def __init__(self, level, discount_rate):
        self.level = level
        self.discount_rate = discount_rate


class Item:
    """商品类"""

    def __init__(self, name, price):
        self.name = name
        self.price = price


class ShoppingCart:
    """购物车类"""

    def __init__(self):
        self.items = []
        self.member = None  # 默认为非会员

    def add_item(self, item):
        self.items.append(item)

    def apply_discount(self):
        """计算折扣后的总价"""
        total = sum(item.price for item in self.items)
        if self.member:
            return total * self.member.discount_rate
        return total

    def show(self):
        """显示购物车信息和折扣详情"""
        print("购物车商品:")
        for item in self.items:
            print(f"- {item.name}: ￥{item.price:.2f}")

        subtotal = sum(item.price for item in self.items)
        print(f"\n小计: ￥{subtotal:.2f}")

        if self.member:
            discount = subtotal - self.apply_discount()
            print(f"会员等级: {self.member.level}")
            print(f"折扣率: {self.member.discount_rate * 100}%")
            print(f"节省金额: ￥{discount:.2f}")

        print(f"总计: ￥{self.apply_discount():.2f}")


# 定义会员等级折扣率（使用装饰器风格）
def member_discount(level):
    """会员折扣装饰器工厂"""

    def decorator(cls):
        """为Member类添加折扣率"""
        if level == "gold":
            return cls(level, 0.8)  # 金卡8折
        elif level == "silver":
            return cls(level, 0.9)  # 银卡9折
        elif level == "bronze":
            return cls(level, 0.95)  # 铜卡95折
        else:
            return cls(level, 1.0)  # 非会员无折扣

    return decorator


# 示例使用
if __name__ == "__main__":
    # 创建商品
    apple = Item("苹果", 5.0)
    milk = Item("牛奶", 10.0)
    bread = Item("面包", 8.0)

    # 创建购物车
    cart = ShoppingCart()
    cart.add_item(apple)
    cart.add_item(milk)
    cart.add_item(bread)

    # 设置会员（金卡会员）
    cart.member = member_discount("gold")(Member)

    # 显示购物车详情（含折扣信息）
    cart.show()